home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / grayimage / vendian_io.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  7.2 KB  |  273 lines  |  [TEXT/R*ch]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *            Verify integer and bit I/O
  6.  *
  7.  ************************************************************************
  8.  */
  9.  
  10. #include "endian_io.h"
  11. #include <ostream.h>
  12. #include <std.h>
  13.  
  14. /*
  15.  *------------------------------------------------------------------------
  16.  *            Reading and checking functions
  17.  */
  18.  
  19. static void read_and_check_long(EndianIO& i_stream, const long ethalon)
  20. {
  21.   long read = i_stream.read_long("Reading a long integer");
  22.   if( read != ethalon )
  23.     _error("The read long int %d differs from what was expected %d",
  24.        read,ethalon);
  25. }
  26.  
  27. static void read_and_check_short(EndianIO& i_stream, const short ethalon)
  28. {
  29.   short read = i_stream.read_short("Reading a short integer");
  30.   if( read != ethalon )
  31.     _error("The read short int %d differs from what was expected %d",
  32.        read,ethalon);
  33. }
  34.  
  35. static void read_and_check_byte(EndianIO& i_stream, const char ethalon)
  36. {
  37.   char read = i_stream.read_byte("Reading a byte");
  38.   if( read != ethalon )
  39.     _error("The read byte %d differs from what was expected %d",
  40.        read,ethalon);
  41. }
  42.  
  43. /*
  44.  *------------------------------------------------------------------------
  45.  *        Reading and writing ethalon patterns
  46.  */
  47.  
  48. const unsigned long Pattern [] =
  49.  { 1, (unsigned)-1, 0, 0xffff0000, 0x0000ffff, 0x5a5a5a5a, 0xa5a5a5a5 };
  50.  
  51. static void write_patterns(EndianIO& o_stream)
  52. {
  53.   {
  54.     unsigned long * p = (unsigned long *)Pattern; 
  55.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  56.       o_stream.write_long(*p);
  57.   }
  58.   {
  59.     unsigned short * p = (unsigned short *)Pattern; 
  60.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  61.       o_stream.write_short(*p);
  62.   }
  63.   {
  64.     unsigned char * p = (unsigned char *)Pattern; 
  65.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  66.       o_stream.write_byte(*p);
  67.   }
  68. }
  69.  
  70. static void read_and_check_patterns(EndianIO& i_stream)
  71. {
  72.   {
  73.     unsigned long * p = (unsigned long *)Pattern; 
  74.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  75.       read_and_check_long(i_stream,*p);
  76.   }
  77.   {
  78.     unsigned short * p = (unsigned short *)Pattern; 
  79.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  80.       read_and_check_short(i_stream,*p);
  81.   }
  82.   {
  83.     unsigned char * p = (unsigned char *)Pattern; 
  84.     for(; (char *)p < (char *)Pattern + sizeof(Pattern); p++)
  85.       read_and_check_byte(i_stream,*p);
  86.   }
  87. }
  88.  
  89. /*
  90.  *------------------------------------------------------------------------
  91.  *            Verify reading/writing integers in
  92.  *          littleendian mode (most significant byte last)
  93.  */
  94.  
  95. static void test_littleendian()
  96. {
  97.   cout << "\n--> Test reading/writing integers in the littleendian mode\n";
  98.  
  99.   cout << "Opening the output stream to file /tmp/aa\n";
  100.   EndianIO stream("/tmp/aa",ios::out);
  101.   assert( stream.writable() && !stream.readable() );
  102.   stream.set_littlendian();
  103.  
  104.   cout << "Writing patterns\n";
  105.   write_patterns(stream);
  106.   stream.close();
  107.  
  108.   cout << "Opening the file as a reading stream through cat\n";
  109.   EndianIO istream;
  110.   istream.open("cat /tmp/aa |",ios::in);
  111.   assert( !istream.writable() && istream.readable() );
  112.   istream.set_littlendian();
  113.  
  114.   cout << "Reading what we've written back\n";
  115.   read_and_check_patterns(istream);
  116.   istream.close();
  117.  
  118.   cout << "\nDone\n";
  119. }
  120.  
  121. /*
  122.  *------------------------------------------------------------------------
  123.  *            Verify reading/writing integers in
  124.  *          bigendian mode (most significant byte first)
  125.  */
  126.  
  127. static void test_bigendian()
  128. {
  129.   cout << "\n--> Test reading/writing integers in the bigendian mode\n";
  130.  
  131.   cout << "Opening the output stream to file /tmp/aa through cat\n";
  132.   EndianIO stream("| cat > /tmp/aa",ios::out);
  133.   assert( stream.writable() && !stream.readable() );
  134.   stream.set_bigendian();
  135.  
  136.   cout << "Writing patterns\n";
  137.   write_patterns(stream);
  138.   stream.close();
  139.  
  140.   cout << "Opening the file as a reading stream straight\n";
  141.   EndianIO istream;
  142.   sleep(1);
  143.   istream.open("/tmp/aa",ios::in);
  144.   assert( !istream.writable() && istream.readable() );
  145.   istream.set_bigendian();
  146.  
  147.   cout << "Reading what we've written back\n";
  148.   read_and_check_patterns(istream);
  149.   istream.close();
  150.  
  151.   cout << "\nDone\n";
  152. }
  153.  
  154. /*
  155.  *------------------------------------------------------------------------
  156.  *        Test the mixed int/bit stream I/O
  157.  */
  158.  
  159. static void test_int_bit_IO()
  160. {
  161.   cout << "\n--> Test mixed int/bit stream I/O with file attachments\n";
  162.  
  163.   cout << "Opening the output stream to file /tmp/aa through cat\n";
  164.   EndianIO stream("| cat > /tmp/aa",ios::out);
  165.   assert( stream.writable() && !stream.readable() );
  166.   stream.set_bigendian();
  167.  
  168.   cout << "Writing integer patterns\n";
  169.   write_patterns(stream);
  170.  
  171.   cout << "Attaching the bitstream\n";
  172.   BitIO bitstream;
  173.   bitstream.open(stream);
  174.   assert( bitstream.writable() && !bitstream.readable() );
  175.  
  176.   cout << "Writing 8 bits of ones followed by 3*8 zero bits several times\n";
  177.   register int i;
  178.   for(i=0; i<5; i++)
  179.   {
  180.     register int i;
  181.     for(i=0; i<8; i++)
  182.       bitstream.put_bit(1);
  183.     for(i=0; i<3*8; i++)
  184.       bitstream.put_bit(0);
  185.   }
  186.   bitstream.put_bit(1);            // Put two extra bits
  187.   bitstream.put_bit(1);
  188.   bitstream.close();
  189.   {
  190.     cout << "Attaching the second bitstream\n";
  191.     BitIO bitstream;
  192.     bitstream.open(stream);
  193.     assert( bitstream.writable() && !bitstream.readable() );
  194.  
  195.     cout << "Writing 8 bits of zeros followed by 3*8 one bits several times\n";
  196.     register int i;
  197.     for(i=0; i<5; i++)
  198.     {
  199.       register int i;
  200.       for(i=0; i<8; i++)
  201.     bitstream.put_bit(0);
  202.       for(i=0; i<3*8; i++)
  203.     bitstream.put_bit(1);
  204.     }
  205.     bitstream.put_bit(0);            // Put two extra bits
  206.     bitstream.put_bit(1);
  207.   }
  208.   stream.close();
  209.  
  210.   cout << "Opening the file as a reading stream straight\n";
  211.   EndianIO istream;
  212.   sleep(1);
  213.   istream.open("/tmp/aa",ios::in);
  214.   assert( !istream.writable() && istream.readable() );
  215.   istream.set_bigendian();
  216.  
  217.   cout << "Reading what we've written back\n";
  218.   read_and_check_patterns(istream);
  219.  
  220.   cout << "Attaching the input bitstream\n";
  221.   BitIO ibitstream;
  222.   ibitstream.open(istream);
  223.   assert( !ibitstream.writable() && ibitstream.readable() );
  224.  
  225.   system("ls -l /tmp/aa; od -x /tmp/aa");
  226.   cout << "Reading the bit pattern\n";
  227.   for(i=0; i<5; i++)
  228.   {
  229.     register int i;
  230.     for(i=0; i<8; i++)
  231.       assert( ibitstream.get_bit() == 1 );
  232.     for(i=0; i<3*8; i++)
  233.       assert( ibitstream.get_bit() == 0 );
  234.   }
  235.   assert( ibitstream.get_bit() == 1 );
  236.   assert( ibitstream.get_bit() == 1 );
  237.   ibitstream.close();
  238.   {
  239.     cout << "Attaching the second input bitstream\n";
  240.     BitIO ibitstream;
  241.     ibitstream.rdbuf()->sync();
  242.     ibitstream.open(istream);
  243.     assert( !ibitstream.writable() && ibitstream.readable() );
  244.  
  245.     cout << "Reading the bit pattern\n";
  246.     for(i=0; i<5; i++)
  247.     {
  248.       register int i;
  249.       for(i=0; i<8; i++)
  250.     assert( ibitstream.get_bit() == 0 );
  251.       for(i=0; i<3*8; i++)
  252.     assert( ibitstream.get_bit() == 1 );
  253.     }
  254.     assert( ibitstream.get_bit() == 0 );
  255.     assert( ibitstream.get_bit() == 1 );
  256.   }
  257.   cout << "\nDone\n";
  258. }
  259.  
  260. /*
  261.  *------------------------------------------------------------------------
  262.  *            Root module
  263.  */
  264.  
  265. main()
  266. {
  267.   cout << "\n\n\t\tVerify integer stream I/O in big/little endian modes"
  268.           "\n\t\t\t\tand bit stream I/O\n\n";
  269.   test_littleendian();
  270.   test_bigendian();
  271.   test_int_bit_IO();
  272. }
  273.